﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using Drill.Configuration;
using Drill.Core.Builders;
using Drill.Extensibility;

namespace Drill.Core.MasterFactories
{
	public class ConfigBasedMasterDependencyResolverFactory : IDependencyResolverFactory
	{
		private readonly IDependencyResolverConfiguration _config;

		public ConfigBasedMasterDependencyResolverFactory(IDependencyResolverConfiguration config)
		{
			if (null == config)
			{
				throw new ArgumentNullException("config");
			}
			_config = config;
		}

		/// <summary>
		/// Creates a new <see cref="IDependencyResolver"/> instance.
		/// </summary>
		/// <returns>A new <see cref="IDependencyResolver"/> instance.</returns>
		public IDependencyResolver CreateDependencyResolver()
		{
			if (false == _config.Enabled)
			{
				return null;
			}

			string assemblyQualifiedTypeName = _config.Type;
			string dependencyResolverName = _config.Name;
			IConfigurationHelperFactory configurationHelperFactory = GetConfigurationHelperFactory();
			IRootPathProviderFactory rootPathProviderFactory = GetRootPathProviderFactory();
			ISearchPathFactory searchPathFactory = GetSearchPathFactory();
			IDrillBitSetFactory drillBitSetFactory = GetDrillBitSetFactory();

			IDependencyResolverBuilder builder =
				new DependencyResolverBuilder(
					dependencyResolverName,
					configurationHelperFactory,
					drillBitSetFactory,
					rootPathProviderFactory,
					searchPathFactory,
					assemblyQualifiedTypeName
					);

			IDependencyResolverBuilderDirector director = new DependencyResolverBuilderDirector();
			director.Initialize(builder);
			return director.Build();
		}

		private IConfigurationHelperFactory GetConfigurationHelperFactory()
		{
			return new ConfigurationHelperFactory();
		}

		private IRootPathProviderFactory GetRootPathProviderFactory()
		{
			var config = _config.RootPathProviders;
			if (null == config || config.Count < 1)
			{
				return null;
			}
			return new ConfigBasedRootPathProviderFactory(config);
		}

		private ISearchPathFactory GetSearchPathFactory()
		{
			var config = _config.SearchPaths;
			if (null == config || config.Count < 1)
			{
				return null;
			}
			return new ConfigBasedSearchPathFactory(config);
		}

		private IDrillBitSetFactory GetDrillBitSetFactory()
		{
			return new ConfigBasedMasterDrillBitSetFactory(_config.DrillBits);
		}
	}
}